package com.xzh.homeliving.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzh.common.utils.PageUtils;
import com.xzh.common.utils.Query;

import com.xzh.homeliving.commodity.dao.AttrAtRelationDao;
import com.xzh.homeliving.commodity.entity.AttrAtRelationEntity;
import com.xzh.homeliving.commodity.service.AttrAtRelationService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author xzh
 */
@Service("attrAtRelationService")
public class AttrAtRelationServiceImpl extends ServiceImpl<AttrAtRelationDao, AttrAtRelationEntity> implements AttrAtRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAtRelationEntity> page = this.page(
                new Query<AttrAtRelationEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void addAttrAndAtRelation(AttrAtRelationEntity[] attrAtRelationEntities) {
        for(AttrAtRelationEntity attrAtRelationEntity : attrAtRelationEntities) {
            // 先得到属性 id
            Long attrId = attrAtRelationEntity.getAttrId();
            AttrAtRelationEntity relationEntity = this.getOne(new QueryWrapper<AttrAtRelationEntity>().eq("attr_id", attrId));
            if(relationEntity == null) {
                this.save(attrAtRelationEntity);
            } else {
                this.update(new UpdateWrapper<AttrAtRelationEntity>().eq("attr_id", attrId).set("attr_group_id", attrAtRelationEntity.getAttrGroupId()));
            }
        }
    }
}